package com.hm.health.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.goodix.comsdk.entity.MeasureResult;
import com.goodix.libromablecom.sdk.cardiff.CardiffConstants;
import com.hm.health.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chenshi on 2019/12/23.
 */

public class MeasureResultAdapter extends RecyclerView.Adapter<MeasureResultAdapter.ResultHolder>{
    private List<MeasureResult> mMeasureResults;
    private Context mContext;
    private int mLayoutResId;

    public MeasureResultAdapter(Context context, List<MeasureResult> measureResults, int layoutResId) {
        mMeasureResults = measureResults;
        mContext = context;
        mLayoutResId = layoutResId;
    }

    @Override
    public ResultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =  LayoutInflater.from(mContext).inflate(mLayoutResId,parent, false);
        return new ResultHolder(view);
    }

    @Override
    public void onBindViewHolder(ResultHolder holder, int position) {
        if (mMeasureResults.size() < position) return;

        MeasureResult result = mMeasureResults.get(position);
        holder.mTvDeviceName.setText(result.mDeviceName);
        holder.mTvValue.setText(result.mValue);
        holder.mTvUnits.setText(result.mUnit);
        holder.mTvTitle.setText(result.mTitle);

        int color = getQtColor(result.mColor);
        holder.mTvValue.setTextColor(color);

        holder.mProperty1.setText("");
        holder.mProperty2.setText("");

        if (result.mProperties != null) {
            if (result.mProperties.length >= 1) {
                if (result.mProperties[0] >= 0) {
                    holder.mProperty1.setText(String.valueOf(result.mProperties[0]));
                } else {
                    holder.mProperty1.setText("");
                }
            }
            if (result.mProperties.length >= 2) {
                if (result.mProperties[1] >= 0) {
                    holder.mProperty2.setText(String.valueOf(result.mProperties[1]));
                } else {
                    holder.mProperty2.setText("");
                }
            }
        }
    }

    private int getQtColor(int colorType) {
        int color = mContext.getResources().getColor(R.color.blacktxtcolor);
        switch (colorType) {
            case CardiffConstants.MeasureResultColor.GREEN:
                color = mContext.getResources().getColor(R.color.green);
                break;
            case CardiffConstants.MeasureResultColor.RED:
                color = mContext.getResources().getColor(R.color.red);
                break;
        }
        return color;
    }

    @Override
    public int getItemCount() {
        if (null == mMeasureResults) return 0;
        return mMeasureResults.size();
    }

    public static class ResultHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_value)
        TextView mTvValue;
        @BindView(R.id.tv_units)
        TextView mTvUnits;
        @BindView(R.id.tv_device_name)
        TextView mTvDeviceName;
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.tv_property_1)
        TextView mProperty1;
        @BindView(R.id.tv_property_2)
        TextView mProperty2;


        public ResultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
